package com.sky.controller.user;


import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@RestController("userDish")
@RequestMapping("user/dish")
@Slf4j
@Api("用户菜品功能")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @return Result<List < DishVO>>
     * @throws
     * @description 用户根据分类id查询菜品
     * @author Administrator
     * @param[1] categoryId
     * @time 2023/12/1 17:00
     */
    //@ApiOperation("用户根据分类id查询菜品")
    //@GetMapping("/list")
    //public Result<List<DishVO>> selectDishByCategoryId(Long categoryId) {
    //
    //    log.info("正在执行用户根据分类id查询菜品.......分类id为{}", categoryId);
    //
    //    ValueOperations valueOperations = redisTemplate.opsForValue();
    //    String key = "user_" + categoryId;
    //    List<DishVO> dishVOS = (List<DishVO>) valueOperations.get(key);
    //
    //    if (dishVOS == null) {
    //        dishVOS = dishService.getDishVO(categoryId);
    //        dishVOS = dishVOS.stream().filter(a ->
    //                a.getStatus() == StatusConstant.ENABLE
    //        ).collect(Collectors.toList());
    //
    //    }
    //    return Result.success(dishVOS);
    //}
    @ApiOperation("用户根据分类id查询菜品")
    @GetMapping("/list")
    @Cacheable(cacheNames = "userCache",key = "#categoryId")
    public Result<List<DishVO>> selectDishByCategoryId(Long categoryId) {

        log.info("正在执行用户根据分类id查询菜品.......分类id为{}", categoryId);

        List<DishVO> dishVO = dishService.getDishVO(categoryId);
        dishVO = dishVO.stream().filter(a ->
                a.getStatus() == StatusConstant.ENABLE
        ).collect(Collectors.toList());

        return Result.success(dishVO);
    }
}
